package com.example.login.controller;


import com.example.login.entity.Forum;
import com.example.login.entity.Reply;
import com.example.login.entity.User;
import com.example.login.repository.ForumRepository;
import com.example.login.repository.ReplyRepository;
import com.example.login.repository.UserRepository;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/forum")
public class ForumController {
    @Resource
    private ForumRepository forumRepository;
    @Resource
    private UserRepository userRepository;
    @Resource
    private ReplyRepository replyRepository;

    @PostMapping("/forum")
    public Map<Integer,Map<String,Forum>> showall(){
        List<Forum> forums = forumRepository.findAll();
        Map<Integer,Map<String,Forum>> map=new HashMap<>();
        for(int i=0;i<forums.size();i++)
        {
            Optional<User> user = userRepository.findById(forums.get(i).getId());
            Map<String, Forum> list = new HashMap<>();
            list.put(user.get().getUsername(),forums.get(i));
            map.put(i,list);
        }
        return map;
    }


    @PostMapping("/myforum/{id}")
    public List<Forum> show(@PathVariable Integer id){
        List<Forum> forumList = forumRepository.findById(id);
        return forumList;
    }

    @PostMapping("/addforum/{id}")
    public Forum AddForum(@RequestBody Forum forum1,@PathVariable Integer id){
        Forum forum=new Forum();
        forum.setId(id);
        forum.setForum_title(forum1.getForum_title());
        forum.setContext(forum1.getContext());
        forum = forumRepository.save(forum);
        return forum;
    }


    @PostMapping("/reply")
    public List findreply(@RequestBody Integer forumid){
        List<Reply> replies= replyRepository.findByForumid(forumid);
        List<Map<String,Reply>> list=new ArrayList<>();
        for(int i=0;i<replies.size();i++){
            Optional<User> user=userRepository.findById(replies.get(i).getUserid());
            Map<String,Reply> map=new HashMap<>();
            map.put(user.get().getUsername(),replies.get(i));
            list.add(i,map);
        }

        System.out.println(list);
        return list;
    }

    @PostMapping("/addreply")
        public Reply AddReply(@RequestBody Reply reply1){
        Reply reply=new Reply();
        reply.setForumid(reply1.getForumid());
        reply.setContext(reply1.getContext());
        reply.setUserid(reply1.getUserid());
        reply = replyRepository.save(reply);
        return reply;
    }

    @PostMapping("/findbyid")
    public  Forum findById(@RequestBody Integer forumid){
        System.out.println(forumid);
        System.out.println(getType(forumid));

        Forum forumList =  forumRepository.findByForumid(forumid);
        System.out.println(forumList);

        return forumList;
    }


//    @PostMapping("/findbyid")
//    public List<Forum> findById(@RequestBody String id){
//        System.out.println(id);
//        Integer forumid = Integer.valueOf(id);
//
//        System.out.println(forumRepository.findByForumid(forumid));
//        return forumRepository.findByForumid(forumid);
//    }

    private static String getType(Object a) {
        return a.getClass().toString();
    }
}
